<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class job extends a_base {

    public function __construct() {
        parent::__construct();
        $this->load->model('m_class');
    }

    public function index() {
        $post = $this->input->post();
        $where = array();
        if($post != false){
            foreach($post as $key=>$val){
                if($val != ''){
                    if($key == 'isrecommend' || $key == 'isclose'){
                        if($val != '-1'){
                            $where['job.'.$key] = $val;
                        }
                    }else{
                        $where['job.'.$key] = $val;
                    }
                }
            }
        }

        $page = $this->input->get('page'); //当前页
        $page = intval($page);
        if ($page < 1) {
            $page = 1;
        }
        $condition = array(
            'from' => 'job as job',
            'where' => $where,
        );
        $cnt = $this->m_public->get_list_count($condition);
        $cnt = $cnt['cnt'];
        $page_size = 20;
        $arr = array(
            'page' => $page, //当前页
            'page_size' => $page_size, //分页总大小
            'total' => $cnt//总记录数
        );
        $start_page = ($page - 1) * $page_size;
        $data['pagination'] = new pagination($arr);
        $condition1 = array(
            'from' => 'job as job',
            'where' => $where,
            'limit' => $page_size,
            'offset' => $start_page,
            'join' => array(
                'admin as admin' => 'job.adminid=admin.adminid',
                'class as class' => 'job.classid=class.classid'
            ),
            'column' => 'job.*,admin.adminname,class.classname'
        );
        $data['list'] = $this->m_public->get_list($condition1);
        if(!empty($where)){
            $data['filter'] = 1;
        }else{
            $data['filter'] = 0;
        }

        $this->load->view('job/index', $data);
    }

    public function job_add($jobid){
        $data['config'] = $this->m_public->getAllInfo('config');
        if($jobid){
            $data['info'] = $this->m_public->getRowInfo('job',array('jobid'=>$jobid));
            $class_info = $this->m_public->getRowInfo('class',array('classid'=>$data['info']['classid']),'configid');
            $data['info']['configid'] = $class_info['configid'];
            $cl_data = array(
                'configid' => $data['info']['configid'],
            );
            $data['classtree'] = $this->m_class->get_class_tree($cl_data);
        }else{
            $data['info'] = array();
        }
        $this->load->view('job/job_add',$data);
    }

    public function job_action($jobid){
        $post = $this->input->post();
        if($jobid){
            if(!isset($post['isrecommend'])){
                $post['isrecommend'] = 0;
            }
            $this->m_public->editInfo(array('jobid'=>$jobid),$post,'job');
        }else{
            $post['adminid'] = $this->session->userdata['user_id'];
            $post['addtime'] = date('Y-m-d H:i:s',time());
            $this->m_public->addRowInfo('job',$post);
        }
        redirect('job/index');
    }

    public function delete_info($url,$parmykey,$table,$ids){
        if(!$ids){
            redirect("job/$url");
        }
        $idArr = explode('.', $ids);
        foreach($idArr as $k=>$v){
            $res = $this->m_public->delInfo($table,array($parmykey=>$v));
        }
        redirect("job/$url");
    }

    public function edit_close($url,$parmykey,$table,$val,$ids){
        if(!$ids){
            redirect(b_url("job/$url"));
        }
        $idArr = explode('.', $ids);
        foreach($idArr as $k=>$v){
            $data['isclose'] = $val;
            $this->m_public->editInfo(array($parmykey=>$v),$data,$table);
        }
        redirect("job/$url");
    }

    function move($ids = '') {
        $post = $this->input->post();
        if($post != false){
            $idArr = explode('.', $ids);
            $info['classid'] = $post['classid'];
            foreach($idArr as $val){
                $this->m_public->editInfo(array('jobid'=>$val),$info,'job');
            }
            redirect('job/index');
        }else{
            $data['config'] = $this->m_public->getAllInfo('config');
            $data['ids'] = $ids;
            $this->load->view('job/move', $data);
        }
    }

    function get_class() {
        $configid = $this->input->get('configid');
        $data = array(
            'configid' => $configid,
        );
        $list = $this->m_class->get_class_tree($data);
        $html = '<option value="">请选择</option>';
        foreach($list as $key=>$val){
            $html .= '<option value="'.$key.'">'.$val.'</option>';
        }
        echo $html;
        /*echo json_encode($list);*/
    }

}
